import { RootState } from '@/types/store'
import { useEffect, useRef } from 'react'
import { useDispatch, useSelector } from 'react-redux'

export function useInitValue<T extends keyof RootState>(
  action: () => void,
  stateName: T
) {
  // 存储 action
  const actionRef = useRef(action)
  const dispatch = useDispatch()
  useEffect(() => {
    dispatch(actionRef.current())
  }, [dispatch])
  const state = useSelector((state: RootState) => state[stateName])
  return state
}

/* export function useInitValue<stateType extends keyof RootState>(
  action: () => void,
  stateFile: stateType
) {
  // 1.获取个人信息
  const dispatch = useDispatch()
  // 2.处理action
  useEffect(() => {
    dispatch(action())
  }, [dispatch, action])
  // 3. 获取数据进行渲染
  const state = useSelector((state: RootState) => state[stateFile])
  return state
} */
